//
//  SPSettingTableViewController.m
//  panzer
//
//  Created by 还带大道 on 2018/10/19.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPSettingTableViewController.h"
#import "SPSettingTableViewCell.h"
#import "LoginViewController.h"
#import "ChangeRoleViewController.h"
#import "AppDelegate.h"

@interface SPSettingTableViewController ()
@property (strong, nonatomic) NSArray<NSString *> *items;
@end

@implementation SPSettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    self.tableView.backgroundColor = RZ_COLOR(245, 245, 245);
    _items = @[@"余额预警",@"即将开始的",@"正在直播中",@"主播聘请情况",@"充值",@"数据统计",@"物流信息"];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SPSettingTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([SPSettingTableViewCell class])];
    
    [self setupTableViewFooterView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}



- (void)setupTableViewFooterView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    UIButton *quitBtn = [UIButton new];
    quitBtn.backgroundColor = [UIColor whiteColor];
    quitBtn.layer.cornerRadius = 4;
    quitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [quitBtn setTitle:@"安全退出" forState:UIControlStateNormal];
    [quitBtn setTitleColor:RZ_COLOR(51, 51, 51) forState:UIControlStateNormal];
    [quitBtn addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:quitBtn];
    [quitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
    }];
    
    UIButton *changeBtn = [UIButton new];
    changeBtn.backgroundColor = [UIColor whiteColor];
    changeBtn.layer.cornerRadius = 4;
    changeBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [changeBtn setTitle:@"切换身份" forState:UIControlStateNormal];
    [changeBtn setTitleColor:RZ_COLOR(51, 51, 51) forState:UIControlStateNormal];
    [changeBtn addTarget:self action:@selector(changeRole) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:changeBtn];
    [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(quitBtn.mas_bottom).offset(20);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
    }];
    
    self.tableView.tableFooterView = view;
    view.height = 200;
}




#pragma mark--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section==0?10:0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SPSettingTableViewCell class])];
    cell.title.text = _items[indexPath.section];
    cell.btn.selected = YES;
    return cell;
}



- (void)loginOut
{
    [[NSNotificationCenter defaultCenter]postNotificationName:STOPTIMENOTO object:nil];
    [XTool SaveDefaultInfo:nil Key:BUSSION_INFO];
    [XTool SaveDefaultInfo:nil Key:Prole];
   // LoginViewController *vc = [[LoginViewController alloc]init];
    [APPDELEGATE goComenLoginWithRoleType:RoleType_SJ targetVc:self];

}


- (void)changeRole
{
    ChangeRoleViewController *vc = [[ChangeRoleViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
